﻿using AutoMapper;
using AutoMapper.Configuration;
using GN.Pay.Application.GatewayApp.Dto.Input;
using GN.Pay.Application.GatewayApp.Dto.Output;
using GN.Pay.Core.Entities;
using GN.Pay.Core.Values.TradeRefund;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Application.Dto
{
    /// <summary>
    /// 
    /// </summary>
    public static class DtoMap
    {
        private static readonly IMapper mapper;
        static DtoMap()
        {
            var cfgExpr = new MapperConfigurationExpression();
            RegiesterDtoMap(cfgExpr);
            var cfg = new MapperConfiguration(cfgExpr);
            mapper = cfg.CreateMapper();
        }

        /// <summary>
        /// 转换
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDestination Map<TSource, TDestination>(TSource source)
        {
            return mapper.Map<TSource, TDestination>(source);
        }


        /// <summary>
        /// 
        /// </summary>
        private static void RegiesterDtoMap(MapperConfigurationExpression cfg)
        {
           

            cfg.CreateMap<PayApplyInput, TradePayApply>();
            cfg.CreateMap<TradePayApply, PayApplyOutput>()
                   .ForMember(d => d.ApplyId, opt => opt.MapFrom(s => s.Id));

            cfg.CreateMap<PayChannelInput, TradePayApplyChannel>();

            cfg.CreateMap<TradePayApplyChannel, PayChannelOutput>()
            .ForMember(d => d.TradeNo, opt => opt.MapFrom(s => s.Id));

            cfg.CreateMap<PayApplyAndChannelInput, TradePayApply>();
            cfg.CreateMap<RefundApplyInput, TradeRefundApplyInfo>();
        }
    }
}
